import pymongo as pg
import datetime
from Text_Analysis import stock_predict_lstm_unit_v1 as spluv
from Crawler import crawler_tushare_new as ctn
class ThreeData(object):
    # 获取集合
    def getCollection(self,dataBaseName,collectionName):
        client = pg.MongoClient('localhost', 27017, connect=False)
        db = client.get_database(dataBaseName)
        collection = db.get_collection(collectionName)
        return collection
    """通过三天交易量连续增长的方法判断"""
    def predictThreeDays(self):
        collection = self.getCollection('Stock_Result', 'stock_predict_threeDatas')
        collectionHistory = self.getCollection('Stock_Result', 'stock_predict_threeDatasHistory')
        nowTime = str(datetime.datetime.today().date())
        if collection.find().count() != 0:
            collection.remove({})
        codeAndNameList = ctn.queryAllStockCodeAndName()
        for item in codeAndNameList:
            # 获取股票代码和名称
            name = item.get("name")
            code = item.get('code')
            stockDate = item.get('date')
            oneList = ctn.queryOneStockHistAllDateData(code)
            oneList2 = ctn.queryOneStockHistAllDateData(code)[len(oneList)-1:len(oneList)]
            oneList1 = ctn.queryOneStockHistAllDateData(code)[len(oneList)-2:len(oneList)-1]
            count = 0
            for i,j in zip(oneList1,oneList2):
                endHighPrice = j.get("high")
                beginPrice = i.get("close")
                endPrice = j.get("close")
                endOpenPrice = j.get("open")
                beginVolume = i.get("volume")
                endVolume = j.get("volume")
                beginV_ma5 = i.get("v_ma5")
                endV_ma5 = j.get("v_ma5")
                beginMa5 = i.get("ma5")
                endMa5 = j.get("ma5")
                beginTurnover = i.get("turnover")
                endTurnover = j.get("turnover")
                endBeginVolumeMin = 0
                endBeginVolumeMax = 1
                endBeginPriceMin = 0
                endBeginPriceMax = 0.1
                endBeginPriceHighClose = 0.00001
                endBeginV_ma5 = 0
                endBeginMa5 = 0
                endOpenBeginPrice = 0.01
                try:
                    # if beginVolume < endVolume and (endVolume-beginVolume) <= beginVolume and (endPrice-beginPrice)/beginPrice >= 0.001 and (endPrice-beginPrice)/beginPrice < 0.09 and (endHighPrice-endPrice)/endPrice <= 0.0005:
                    if (endVolume-beginVolume) / beginVolume >= endBeginVolumeMin \
                            and (endVolume - beginVolume) / beginVolume <= endBeginVolumeMax \
                            and (endPrice - beginPrice) / beginPrice >= endBeginPriceMin \
                            and (endPrice - beginPrice) / beginPrice <= endBeginPriceMax \
                            and (endHighPrice - endPrice) / endPrice <= endBeginPriceHighClose \
                            and (endOpenPrice - beginPrice) / beginPrice <= endOpenBeginPrice \
                            and (endV_ma5 - beginV_ma5) / beginV_ma5 >= endBeginV_ma5 \
                            and endTurnover > beginTurnover >= 1:

                        count = count + 1
                except BaseException:
                    continue
            # if count == 2 and ctn.getSZZSByUrl()[1] >= ctn.getSZZSByUrl()[0]:
            if count == 1:
                threeResult = '预计短期上涨'
                collection.insert({'date': nowTime,'stockDate':stockDate, 'code': code, 'name': name, 'result': threeResult})
                collectionHistory.insert({'date': nowTime,'stockDate':stockDate, 'code': code, 'name': name, 'result': threeResult})
                continue
            else:
                continue
